package com.ui_componet.list;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;


import com.ui_componet.R;
import com.ui_componet.list.interfaces.BindNetViewModeInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tanghongbin on 2017/3/10.此布局及其子类在嵌套布局中使用
 */

public abstract class BindingNestedNetView<VIEW extends AbsListView> extends FrameLayout {


    private static final String NO_CONTENT_VIEW = "no_content_view";
    private List mList = new ArrayList();
    private List itemList;
    private int pageNo = 1;//初始页开始数量
    private int initPage = pageNo;//用来记住最开设置的启动页数，方便后面重置状态时使用
    private TextView no_content;
    private OnClickListener noContentClick;
    protected int pageSize = 10;
    protected static Class<? extends CouldClickView> noContentClass;//没有内容显示的默认视图
    private BindNetViewModeInterface modeInterface;

    public BindNetViewModeInterface getModeInterface() {
        return modeInterface;
    }

    public void setModeInterface(BindNetViewModeInterface modeInterface) {
        this.modeInterface = modeInterface;
    }

    public static void setNoContentClass(Class<? extends CouldClickView> noContentClass) {
        BindingNestedNetView.noContentClass = noContentClass;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        refreshView.setOnItemClickListener(onItemClickListener);
    }


    public BindingNestedNetView(Context context) {
        super(context);
        init(context);
    }

    public BindingNestedNetView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;

        initPage = pageNo;

    }

    private Mode mMode = Mode.BOTH;

    protected VIEW refreshView;
    protected BaseAdapter adapter;


    /**
     * 上下文环境对象
     */
    protected Context mContext;
    /**
     * 没有内容时显示的view
     */
    protected View noContentView;


    public BaseAdapter getAdapter() {
        return adapter;
    }


    public View getOriginalView() {
        return this;
    }

    //初始化
    protected final void init(Context context) {
        mContext = context;
        refreshView = generateBindView();

        refreshView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(refreshView);
    }

    /**
     * 生成绑定的view
     *
     * @return
     */
    protected abstract VIEW generateBindView();

    public void setAdapter(BaseAdapter adapter) {
        this.adapter = adapter;
        refreshView.setAdapter(adapter);
    }

    public void setNoContentView(View view) {
        if (noContentView != null) {
            removeView(noContentView);
        }
        this.noContentView = view;
        bindingListChanged();
    }

    /**
     * 通知数据发生改变
     */
    public void notifyObserverDataChanged() {
        if (modeInterface != null){
            modeInterface.onRefreshCompelete();
        }
        if (adapter == null) {
            throw new NullPointerException("adater 不能为空");
        }
        adapter.notifyDataSetChanged();
        bindingListChanged();
    }


    /**
     * 绑定的集合数据发生变化，自动映射到listview
     */
    private void bindingListChanged() {
        //总数量集合为空时，填充默认视图并且隐藏refreshview
        if (mList == null || mList.size() == 0) {
            refreshView.setVisibility(GONE);
            if (findViewWithTag(NO_CONTENT_VIEW) == null) {
                if (noContentView == null) {
                    noContentView = getnerateDefaultView(mContext);
                }
                noContentView.setTag(NO_CONTENT_VIEW);
            }
            if (noContentView.getParent() != null) {
                ViewGroup group = (ViewGroup) noContentView.getParent();
                group.removeView(noContentView);
            }
            this.addView(noContentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            noContentView.setVisibility(View.VISIBLE);
            return;
        }
        //如果有数据，并且布局里面的视图有noContentView，那么隐藏noContentView，重新显示ListView
        if (mList.size() > 0) {
            View view = findViewWithTag(NO_CONTENT_VIEW);
            if (view != null) {
                noContentView.setVisibility(View.GONE);
                refreshView.setVisibility(View.VISIBLE);
            }
        }

        //每次请求的集合数量
        if (itemList == null || itemList.size() == 0) {
            mMode = Mode.BOTH;
            if (modeInterface != null) {
                modeInterface.pullFromStart();
            }
            return;
        }else {

        }

        if (itemList.size() < pageSize) {
            mMode = Mode.PULL_FROM_START;
            if (modeInterface != null) {
                modeInterface.pullFromStart();
            }
        }

        if (itemList.size() >= pageSize) {
            mMode = Mode.BOTH;
            pageNo++;
            if (modeInterface != null) {
                modeInterface.both();
            }
        }


    }

    /**
     * 重置view的显示状态，显示refreshview，隐藏noContentView,并且将刷新模式设置为BOTH
     */
    protected void resetViewStatus() {
        refreshView.setVisibility(VISIBLE);

        if (noContentView != null) {
            noContentView.setVisibility(GONE);
        }

    }

    /**
     * 当默认显示内容为空时，填充默认视图
     */
    private View getnerateDefaultView(Context context) {

        View view = null;
        if (noContentClass != null) {
            try {
                view = noContentClass.newInstance();
                CouldClickView clickableView = (CouldClickView) view;
                clickableView.setNoContentClickListener(noContentClick);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("TAG", "无内容视图初始化失败");
            }
        } else {
            view = View.inflate(mContext, R.layout.no_content_view, null);
            no_content = (TextView) view.findViewById(R.id.no_content);
            if (noContentClick != null) {
                no_content.setOnClickListener(noContentClick);
            }
        }

        return view;
    }

    //外界只需要调用此方法维护集合
    public void bindList(List itemList) {
        this.itemList = itemList;
        if (pageNo == initPage) {
            mList.clear();
        }
        if (itemList != null && itemList.size() > 0) {
            mList.addAll(itemList);
        }

    }

    /**
     * 清空集合
     */
    public void clearContainer() {
        mList.clear();
    }


    public List getmList() {
        return mList;
    }

    public int resetPage() {
        pageNo = initPage;
        return pageNo;
    }

    /**
     * 获取刷新模式
     *
     * @return
     */
    public Mode getmMode() {
        return mMode;
    }

    public void setNoContentClick(OnClickListener contentClick) {
        this.noContentClick = contentClick;
    }

    public enum Mode {
        BOTH, PULL_FROM_START, PULL_FORM_END
    }
}

